package com.zhuangfei.hpuhelper.tools;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.hpu.bean.News;
import com.zhuangfei.hpuhelper.view.MyApplication;
import com.zhuangfei.toolkit.tools.ToastTools;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;

/**
 * @author Administrator
 * 
 */
public class FileTools {

	public static final String ROOT = "/zfapp";

	public static String SOFT = "/hpucampus";

	public static String ROOM = "/room";

	public static String NOTICE = "/notice";

	public static String IMAGE = "/image";
	public static String COURSE = "/course";
	public static String LOG = "/log";

	public static File saveViewImage(Bitmap bitmap, String fileName) {
		File file = new File(getDir(IMAGE), fileName);
		try {
			FileOutputStream fos = new FileOutputStream(file);
			bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);
			fos.flush();
			fos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return file;
	}

	public static void clearRoom() {
		File file = new File(getDir(""));
		if (!file.exists())
			file.mkdirs();
		File[] files = file.listFiles();
		if (files != null) {
			for (int i = 0; i < files.length; i++) {
				if (files[i].isDirectory()) {
					File file2 = new File(getDir(files[i].getName() + "/room"));
					if (file2 != null) {
						File[] files2 = file2.listFiles();
						if (files2 != null) {
							for (int m = 0; m < files2.length; m++) {
								files2[m].delete();
							}
						}
					}

				}
			}
		}

	}

	private static boolean checkSdCard() {
		if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
			return true;
		else
			return false;
	}

	private static String getSdCard() {
		return Environment.getExternalStorageDirectory() + "/";
	}

	public static String getDir(String dir) {
		String dirPath = getSdCard() + ROOT + SOFT + "/" + dir;
		File file = new File(dirPath);
		if (!file.exists())
			file.mkdirs();
		return dirPath;
	}

	public static void createFileDir(String fileDir) {
		String path = getDir(fileDir);
		File file = new File(path);
		if (!file.exists())
			file.mkdirs();
	}

	/**
	 * 将空教室信息写入本地
	 * 
	 * @param number
	 * @param roomName
	 * @param roomInfo
	 */
	public static void writeRoom(String number, String roomName, String roomInfo) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		File file = new File(getDir(number + ROOM) + "/" + roomName + "_" + sdf.format(new Date()) + ".txt");

		try {
			PrintStream ps = new PrintStream(file);
			ps.println(roomInfo);
			ps.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public static File getLogFile(String number) {
		if(number==null) number="default";
		 //用于格式化日期,作为日志文件名的一部分  
		DateFormat formatter = new SimpleDateFormat("yyyyMMdd-HHmmss");  
		long timestamp = System.currentTimeMillis();  
        String time = formatter.format(new Date());  
		String fileName = "crash-" + time + "-" + timestamp +"-"+number+".log"; 
		File file = new File(getDir(LOG) + "/" + fileName);
		return file;
	}
	
	/**
	 * 将空教室信息写入本地
	 * 
	 * @param number
	 */
	public static void writeCourse(String number, String term, String content) {
		File file = new File(getDir(number + COURSE) + "/" + term + ".txt");

		try {
			PrintStream ps = new PrintStream(file);
			ps.println(content);
			ps.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	public static void writeLog(String logName, String content) {
		File file = new File(getDir(LOG) + "/" + logName + ".txt");
		try {
			PrintStream ps = new PrintStream(file);
			ps.println(content);
			ps.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	
	public static void saveImage(String number,Bitmap bitmap) {
		File file = new File(getDir(IMAGE) + "/" + number + ".png");

		try {
			FileOutputStream fos = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
            fos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	
	public static Bitmap getImage(String number) {
		File file = new File(getDir( IMAGE) + "/" + number + ".png");

		if(!file.exists()) return null;
		Bitmap bitmap = null;
        try{
        	bitmap = BitmapFactory.decodeFile(getDir( IMAGE) + "/" + number + ".png");
        	return bitmap;
        } catch (Exception e) {
        	return null;
        }

	}
	

	public static String readCourse(String number, String term) {
		File file = new File(getDir(number + COURSE) + "/" + term + ".txt");
		String result = "", line = "";
		try {
			if (!file.exists())
				file.createNewFile();
			FileInputStream fis = new FileInputStream(file);
			InputStreamReader isd = new InputStreamReader(fis);
			BufferedReader reader = new BufferedReader(isd);
			while ((line = reader.readLine()) != null) {
				result += "\n" + line;
			}
			reader.close();
			isd.close();
			fis.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 读取该用户今天的空教室
	 * 
	 * @param number
	 * @param roomName
	 * @return
	 */
	public static String readRoom(String number, String roomName) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String result = "", line = "";
		File file = new File(getDir(number + ROOM) + "/" + roomName + "_" + sdf.format(new Date()) + ".txt");

		try {
			if (!file.exists())
				file.createNewFile();
			FileInputStream fis = new FileInputStream(file);
			InputStreamReader isd = new InputStreamReader(fis);
			BufferedReader reader = new BufferedReader(isd);
			while ((line = reader.readLine()) != null) {
				result += "\n" + line;
			}
			reader.close();
			isd.close();
			fis.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 读取该用户今天的空教室
	 *
	 * @return
	 */
	public static String readMessage(String title) {
		createFileDir(NOTICE);
		String result = "", line = "";
		File file = new File(getDir(NOTICE + "/" + title));

		try {
			if (!file.exists())
				file.createNewFile();
			FileInputStream fis = new FileInputStream(file);
			InputStreamReader isd = new InputStreamReader(fis);
			BufferedReader reader = new BufferedReader(isd);
			while ((line = reader.readLine()) != null) {
				result += "\n" + line;
			}
			reader.close();
			isd.close();
			fis.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}

	public static String findMessageFileName(String title) {
		File file = new File(getDir(NOTICE));
		if (file.exists())
			file.mkdirs();
		File[] files = file.listFiles();
		if (files != null) {
			for (int i = 0; i < files.length; i++) {
				String name = files[i].getName();
				int index = -1;
				if ((index = name.lastIndexOf(".")) != -1) {
					if (title.equals(name.substring(0, index)))
						return name;
				}
			}
		}

		return null;
	}
}
